﻿#ifndef MYRP_UNLIT_INCLUDED
#define MYRP_UNLIT_INCLUDED

	#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"	

	CBUFFER_START (UnityPerDraw)
		float4x4 unity_ObjectToWorld;
	CBUFFER_END

	#define UNITY_MATRIX_M unity_ObjectToWorld
	#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"

	CBUFFER_START (UnityPerFrame)
		float4x4 unity_MatrixVP;
	CBUFFER_END

	sampler2D _ShadowMap;
	UNITY_INSTANCING_BUFFER_START(perinstance)	
		UNITY_DEFINE_INSTANCED_PROP(float4,_Color)
	UNITY_INSTANCING_BUFFER_END(perinstance)

	CBUFFER_START (_ShadowBuffer)
		float4x4 _WorldToShadowMatrix;
	CBUFFER_END

	float _ClipValue;
	struct VertexInput{
		float4 pos :POSITION;
		UNITY_VERTEX_INPUT_INSTANCE_ID
	};
	struct VertexOutput{
		float4 clipPos :SV_POSITION;
		float4 worldPos : TEXCOORD0;
		float4 clip :TEXCOORD1;
		UNITY_VERTEX_INPUT_INSTANCE_ID
	};

	float3 GetLightDirClipPos(float3 worldPos)
	{
		float4 p= mul(_WorldToShadowMatrix,float4(worldPos,1));
		p.xyz= p.xyz/p.w;
		p.xyz	= p.xyz+0.5*0.5;
		return p;
	}

	VertexOutput vert(VertexInput i){
		VertexOutput v ;	
		UNITY_SETUP_INSTANCE_ID(i);
		UNITY_TRANSFER_INSTANCE_ID(i,v);
		
		float4 worldPos = mul(UNITY_MATRIX_M, float4(i.pos.xyz, 1.0));
		v.worldPos = worldPos;
		v.clipPos = mul(unity_MatrixVP,worldPos);
		v.clip = mul(unity_MatrixVP,worldPos);
		return v;
	}

	float4 frag (VertexOutput o):SV_TARGET{
		UNITY_SETUP_INSTANCE_ID(o);

		o.clip.xyz/=o.clip.w;
		float depth = o.clip.z;
		float4 pos = mul(_WorldToShadowMatrix,float4(o.worldPos.xyz,1));
		pos.xyz /= pos.w;
		pos.xyz=pos.xyz*0.5+0.5f;
		pos.z=1-pos.z;


		float shadowMap = tex2D(_ShadowMap,pos.xy).r;
		return pos.z>shadowMap?1:0;
		
		o.clip.xyz= o.clip.xyz*0.5+0.5;


		return tex2D(_ShadowMap,o.clip.xy).r;


	}


#endif 
